package cn.qn.controller.app;

import cn.qn.base.PageVO;
import cn.qn.dto.ResponseDTO;
import cn.qn.dto.article.ArticleAddDTO;
import cn.qn.dto.article.ArticleDTO;
import cn.qn.dto.article.ArticleQueryDTO;
import cn.qn.dto.user.AppUserAddDTO;
import cn.qn.dto.user.AppUserDTO;
import cn.qn.exceptions.BizException;
import cn.qn.framwork.security.MemberAccountUtils;
import cn.qn.service.article.ArticleService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

/**
 * @author: Bryant
 * @date: 2019/7/19
 * @description:
 */
@RestController
@RequestMapping("/app/article")
public class ArticleController {

    @Reference(check = false, timeout = 30000)
    private ArticleService articleService;

    @RequestMapping(value = "/list", name = "文章列表接口", method = RequestMethod.GET)
    public ResponseDTO list(ArticleQueryDTO query) {
        PageVO<ArticleDTO> appUsers = articleService.pageQueryArticles(query);
        return ResponseDTO.success(appUsers);
    }

    @RequestMapping(value = "/detail/{articleId}" ,name="文章详情接口",method = RequestMethod.GET)
    public ResponseDTO detail(@PathVariable(value = "articleId") String articleId){
        ArticleDTO article = articleService.queryDetailByArticleId(articleId);
        return ResponseDTO.success(article);
    }

    @RequestMapping(value = "/add" ,name="发布文章",method = RequestMethod.POST)
    public ResponseDTO publishArticle(@RequestBody ArticleAddDTO dto){
        try {
            dto.setCreateBy(MemberAccountUtils.getCurrentMember().getUsername());
            articleService.publishArticle(dto);
            return ResponseDTO.success();
        }catch (BizException e){
            return ResponseDTO.error(e.getMessage());
        }
    }

    @RequestMapping(value = "/edit/status/{articleId}" ,name="修改文章状态接口",method = RequestMethod.POST)
    public ResponseDTO editArticleStatus(@PathVariable(value = "articleId") String  articleId){
        try {
            articleService.editArticleStatus(articleId,MemberAccountUtils.getCurrentMember().getUsername());
            return ResponseDTO.success();
        }catch (BizException e){
            return ResponseDTO.error(e.getMessage());
        }
    }

    @RequestMapping(value = "/edit/setTop/{articleId}" ,name="修改文章置顶状态接口",method = RequestMethod.POST)
    public ResponseDTO setTopStatusChange(@PathVariable(value = "articleId") String  articleId){
        try {
            articleService.setTopStatusChange(articleId,MemberAccountUtils.getCurrentMember().getUsername());
            return ResponseDTO.success();
        }catch (BizException e){
            return ResponseDTO.error(e.getMessage());
        }
    }

    @RequestMapping(value = "/delete/{articleId}" ,name="删除文章",method = RequestMethod.POST)
    public ResponseDTO deleteArticle(@PathVariable(value = "articleId") String  articleId){
        try {
            articleService.deleteArticle(articleId);
            return ResponseDTO.success();
        }catch (BizException e){
            return ResponseDTO.error(e.getMessage());
        }
    }
}
